﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Micrhard.FMCG.Server.Interface;
using Micrhard.FMCG.Server.DataAccess;
using Microhard.Core.DBUtility;
using Micrhard.FMCG.Server.DTO;
using Microhard.Core;
using Micrhard.FMCG.Server.Msg;
using Micrhard.FMCG.Server.Models;
using Microhard.Core.Net;

namespace Micrhard.FMCG.Server.Impl
{
    public class CheckInImpl : ICheckin
    {
        public string GeneratePrdDisplyCode()
        {
            Random r = new Random();
            int result = r.Next(100000, 999999);
            return result.ToString();
        }

        public Microhard.Core.DBUtility.ExecutedResult Add(Models.CheckinModel model)
        {
            ExecutedResult rtn = new ExecutedResult();
            MicrhardFMCGServerEntities db = new MicrhardFMCGServerEntities();
            var op = model.ToEntity();
            op.CreateTime = DateTime.Now;
            op.UpdateTime = DateTime.Now;
            model.SubKACode = op.SubKACode = this.GeneratePrdDisplyCode();
            var emp = db.Employees.SingleOrDefault(e => e.UserName == this.scope.CurrentUserName);
            op.EmployeeID = emp.EmployeeID;
            if (model.SubKAsForCheckIn != null && model.SubKAsForCheckIn.Count > 0)
            {
                op.SubKAID = model.SubKAsForCheckIn.First();
            }
            else
            {
                op.SubKAID = Guid.Empty;
            }

            try
            {
                db.EmployeeCheckIns.AddObject(op);
                db.SaveChanges();
            }
            catch (System.Exception ex)
            {
                rtn.ErrorMsg.Add(DBOperationFailedStatus.AddFailed, CheckInMsg.AddFailed);
                Log.Error("ICheckin.Add", ex);
            }
            return rtn;
        }

        public Microhard.Core.DBUtility.ExecutedResult Update(Models.CheckinModel model)
        {
            throw new NotImplementedException();
        }

        public Microhard.Core.DBUtility.ExecutedResult Delete(List<Guid> ids)
        {
            throw new NotImplementedException();
        }

        public Microhard.Core.DBUtility.ExecutedResult Delete(Models.CheckinModel model)
        {
            throw new NotImplementedException();
        }

        public Models.CheckinModel GetModelByID(Guid id)
        {
            throw new NotImplementedException();
        }

        public Models.CheckinModel GetModelByModel(Models.CheckinModel model)
        {
            throw new NotImplementedException();
        }

        public List<Models.CheckinModel> Query(Models.CheckinModel Model, int pageIndex, int pageSize, out int totalCount)
        {
            MicrhardFMCGServerEntities db = new MicrhardFMCGServerEntities();
            var cmp = db.Employees.SingleOrDefault(e => e.UserName == this.scope.CurrentUserName);
            var now = Model.CreateTime.Date;
            var end = now.AddDays(1);
            IEnumerable<EmployeeCheckIn> entities = from b in db.EmployeeCheckIns
                                                    where b.Employee.CompanyID == cmp.CompanyID
                                                    && b.Employee.UserName == this.scope.CurrentUserName
                                                    && b.CreateTime >= now && b.CreateTime <= end
                                                    select b;
            if (cmp.Range == (int)Range.Manager)
            {
                entities = from b in db.EmployeeCheckIns
                           where b.CreateTime >= now && b.CreateTime <= end
                           && b.Employee.CompanyID == cmp.CompanyID
                            && b.CreateTime >= now && b.CreateTime <= end
                           select b;
            }
            totalCount = entities.Count();
            var pagedEntities = entities.OrderByDescending(p => p.CreateTime).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
            if (pagedEntities != null && pagedEntities.Count > 0)
                return pagedEntities.Select(m => m.ToModel()).ToList();
            return new List<CheckinModel>();
        }

        public List<Models.CheckinModel> Query(Models.CheckinModel Model)
        {
            throw new NotImplementedException();
        }

        public Microhard.Core.DBUtility.ScopeResult scope
        {
            set;
            get;
        }



        public bool IsDuplicateOnDay(CheckinModel model)
        {
            if (model.SubKAsForCheckIn == null)
                return false;
            var now = DateTime.Now.Date;
            var end = now.AddDays(1).Date;
            MicrhardFMCGServerEntities db = new MicrhardFMCGServerEntities();
            var ckSubkaid = model.SubKAsForCheckIn.First();
            var cnt = db.EmployeeCheckIns.Count(c => c.SubKAID == ckSubkaid && c.CreateTime >= now && c.CreateTime <= end);
            return cnt > 0 ? true : false;
        }
    }
}
